package com.landmark;

import java.io.File;
import java.io.FileWriter;
import java.util.Arrays;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class RenameVFImageWearImageFiles extends JPanel {

    public void start() {
        HelperBean helperBean = Util.getHelperBean();
        String folderPath = helperBean.getFolderPath(); // images folder
        try {

            Util.createOutputFile("RenameVFImage");
            fw = new FileWriter(Util.getHelperBean().getOutputFile());
            fwErrorFile = new FileWriter(Util.getHelperBean().getErrorFile());

            String[] dir = new java.io.File(folderPath).list();
            java.util.Arrays.sort(dir);
            File f[] = new File(folderPath).listFiles();

            int len1 = dir.length;
            int thumbNailCount = 0;
            String replaceWith = null;
            String[] str = {"VF_AC_", "VF_BK_", "VF_HS_", "VF_RK_", "VF_TF_", "VF_LG_", "VF_CD_", "VF_LE_"};
            Vector<String> vec = new Vector<String>();
            vec.addAll(Arrays.asList(str));
            for (int i = 0; i < len1; i++) {
                String name = f[i].getName();
                replaceWith = null;
                if (name.toUpperCase().startsWith("VF_") && (name.toUpperCase().contains("MOD_LG") || name.toUpperCase().contains("MOD_XL"))) {
                    String firstChars = name.toUpperCase().substring(0, 6);
                    fw.append("\nVector conatins : " + vec.contains(firstChars));
                    if (vec.contains(firstChars)) {
                        fw.append("\n" + name);
                        fw.flush();
                        replaceWith = "VF_" + name.substring(6, name.length());
                    }
                }
                if (replaceWith != null && !"".equalsIgnoreCase(replaceWith)) {
                    File newFileName = new File(f[i].getParentFile(), replaceWith);
                    boolean stat = f[i].renameTo(newFileName);
                    fw.append("\t" + stat);
                    fw.flush();
                }
            }
            System.out.println("Total files converted : " + (len1 - thumbNailCount));
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, e.getCause());
            try {
                fw.append(e.getMessage());
                fw.flush();
            } catch (Exception e1) {
            }
        }

    }
    FileWriter fw = null;
    FileWriter fwErrorFile = null;
}
